<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 17/12/28
 * Time: 10:04
 */

Yaf_Loader::import(EXTEND_PATH.'/qcos/Api.php');

class Cos
{
    private $config = [
        'app_id' => '1253124583',
        'secret_id' => 'AKIDBGZmyTg8B7vYbPNgyBC6pRq3FE1xNUzI',
        'secret_key' => '1FMI2bO3Xo5EB2qZxQjqW4UikcM2bzFK',
        'region' => 'bj',   // bucket所属地域：华北 'tj' 华东 'sh' 华南 'gz'
        'timeout' => 60
    ];

    private $bucket = BUCKET;

    //上传文件
    public function upload($src,$dst)
    {
        $cosApi = new Api($this->config);
        $ret = $cosApi->upload($this->bucket, $src, $dst);
        return $ret;
    }

    //创建文件夹
    public function createFolder($folder)
    {
        $cosApi = new Api($this->config);
        $ret = $cosApi->createFolder($this->bucket, $folder);
        return $ret;
    }

    //下载文件
    public function download($src,$dst)
    {
        $cosApi = new Api($this->config);
        $ret = $cosApi->download($this->bucket,$src,$dst);
        return $ret;
    }

    //列出文件夹
    public function listFolder($folder)
    {
        $cosApi = new Api($this->config);
        $ret = $cosApi->listFolder($this->bucket, $folder);
        return $ret;
    }

    //更新文件夹信息
    public function updateFolder($folder)
    {
        $bizAttr = "";
        $cosApi = new Api($this->config);
        $ret = $cosApi->updateFolder($this->bucket,  $folder, $bizAttr);
        return $ret;
    }

    //更新文件信息
    public function update($dst)
    {
        $bizAttr = '';
        $authority = 'eWPrivateRPublic';
        $customerHeaders = array(
            'Cache-Control' => 'no',
            'Content-Type' => 'application/pdf',
            'Content-Language' => 'ch',
        );
        $cosApi = new Api($this->config);
        $ret = $cosApi->update($this->bucket, $dst, $bizAttr,$authority, $customerHeaders);
        return $ret;
    }

    //统计文件夹
    public function statFolder($folder)
    {
        $cosApi = new Api($this->config);
        $ret = $cosApi->statFolder($this->bucket, $folder);
        return $ret;
    }

    //统计文件
    public function stat($path)
    {
        $cosApi = new Api($this->config);
        $ret = $cosApi->stat($this->bucket, $path);
        return $ret;
    }

    //复制文件
    public function copyFile($src,$dst)
    {
        $cosApi = new Api($this->config);
        $ret = $cosApi->copyFile($this->bucket,$src,$dst);
        return $ret;
    }

    //移动文件
    public function moveFile($src,$dst)
    {
        $cosApi = new Api($this->config);
        $ret = $cosApi->moveFile($this->bucket,$src,$dst);
        return $ret;
    }

    //删除文件
    public function delFile($path)
    {
        $cosApi = new Api($this->config);
        $ret = $cosApi->delFile($this->bucket,$path);
        return $ret;
    }

    //删除文件夹
    public function delFolder($folder)
    {
        $cosApi = new Api($this->config);
        $ret = $cosApi->delFolder($this->bucket, $folder);
        return $ret;
    }
}